package com.neusoft.EYSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.EYSystem.dao.ClientDAO;
import com.neusoft.EYSystem.dao.ClientNursingProjectDAO;
import com.neusoft.EYSystem.dao.NursingLevelDAO;
import com.neusoft.EYSystem.po.*;
import com.neusoft.EYSystem.service.ClientNursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/clientNursingProject")
public class ClientNursingProjectController {
    // ✅ 工具方法：判断是否为合法字符串（排除 null、空串、"null"）
    private boolean isValid(String value) {
        return value != null && !value.trim().isEmpty() && !"null".equalsIgnoreCase(value.trim());
    }


    @Autowired
    private ClientNursingProjectService clientNursingProjectService;

    @Autowired
    private ClientNursingProjectDAO clientNursingProjectDAO;

    @Autowired
    private NursingLevelDAO nursingLevelDAO;

    @Autowired
    private ClientDAO clientDAO;
    // 管理员端 护工管理 服务关注 表格显示
    // 显示客户id，客户名及其护理级别表
    // 支持根据客户名查询
    @GetMapping("/pageClientAndLevel")
    public PageResponseBean<List<ClientAndNursingLevel>> pageClientNursingProject(Long cur, Long number, Client client) {
        return clientNursingProjectService.pageClientNursingProject(cur, number, client);
    }
    //健康管家端只能看到这个管家对应的客户的数据
    // 健康管家端 护理管理 日常护理 表格显示
    // 健康管家端 膳食管理 膳食安排 点击新建膳食安排 进入 表格显示
    // 显示客户id，客户名及其护理级别表
    // 支持根据客户名查询
    @GetMapping("/pageClientAndLevelByCaretaker")
    public PageResponseBean<List<ClientAndNursingLevel>> pageClientNursingProjectByCaretaker(Long cur, Long number, Client client,User user) {
         return clientNursingProjectService.pageClientNursingProjectByCaretaker(cur, number, client,user);
    }

    // 健康管家端 护理管理 日常护理 客户护理项目表格显示
    // 显示客户id，客户名及其已购护理项目表
    // 全部显示 client只有客户名、
    // 按状态筛选的
    @GetMapping("/pageClientAndProject")
    public PageResponseBean<List<ClientAndProject>> pageClientAndProject(Long cur, Long number, String clientName,String projectName) {
        Client client = new Client();
        NursingProject nursingProject = new NursingProject();
        if (isValid(clientName)) {
            client.setName(clientName);
        }
        if (isValid(projectName)) {
            nursingProject.setName(projectName);
        }
        return clientNursingProjectService.pageClientAndProject(cur, number, client, nursingProject);
    }

    // 管理员端 护工管理 服务关注 已购护理项目表格显示 弹窗管理护理项目
    // 管理员端 护工管理 服务关注 购买护理项目界面 下表
    // 按客户名查询其已购护理项目表
    @GetMapping("/pageClientAndProjectByName")
    public PageResponseBean<List<ClientAndProject>> pageClientAndProjectByName(Long cur, Long number, Client client) {
        return clientNursingProjectService.pageClientAndProjectByName(cur, number, client);
    }

    // 管理员端 护工管理 服务关注 购买护理项目界面 上表
    // 按护理级别和客户名查询其未购买护理项目表
    @GetMapping("/pageNursingProject")
    public PageResponseBean<List<NursingProject>> pageNursingProject(Long cur, Long number, Client client, NursingLevel level) {
        return clientNursingProjectService.pageNursingProject(cur, number, client, level);
    }

    // 管理员端：护理管理客户护理设置 表格删除按钮
    // 管理员端 护工管理 服务关注 已购护理项目表格 弹窗管理护理项目 右侧移除按钮
    @PostMapping("/deleteClientNursingProject")
    public ResponseBean<Integer> deleteClientNursingProject(ClientNursingProject clientNursingProject) {
        Integer result;
        ResponseBean<Integer> rb = null;
        try {
            result = clientNursingProjectService.deleteStatusById(clientNursingProject);
            rb =new ResponseBean<>(result);

        } catch (Exception e) {
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除客户及其护理等级及其护理项目错误");
        }
        return rb;
    }

    // 管理员端 护工管理 服务关注 已购护理项目表格 弹窗管理护理项目 右侧移除按钮
    @PostMapping("/deleteClientNursingProjectById")
    public ResponseBean<Integer> deleteClientNursingProjectById(ClientNursingProject clientNursingProject) {
        Integer result;
        ResponseBean<Integer> rb = null;
        try {
            result = clientNursingProjectService.deleteStatusOneById(clientNursingProject);
            rb =new ResponseBean<>(result);

        } catch (Exception e) {
            e.printStackTrace();
            rb =new ResponseBean<>(500,"删除客户及其护理等级及其护理项目错误");
        }
        return rb;
    }

    // 管理员端：护理管理 客户护理设置  为客户配置护理级别及其项目
    // 管理员端 护工管理 服务关注 弹窗购买服务项目 上表右侧购买按钮
    @PostMapping("/addClientNursingProject")
    public ResponseBean<Integer> addClientNursingProject(ClientNursingProject clientNursingProject) {
        ResponseBean<Integer> response = new ResponseBean<>();
        clientNursingProject.setStatus("1");
        try {
            if(clientNursingProjectDAO.insert(clientNursingProject) > 0){
                response.setStatus(200);
                response.setMsg("客户及其护理等级及其护理项目添加成功");
                return response;
            }else {
                response.setStatus(400);
                response.setMsg("客户及其护理等级及其护理项目添加失败，可能是数据不完整或重复");
            }
        } catch (Exception e) {
            response.setStatus(500);
            response.setMsg("客户及其护理等级及其护理项目添加失败: " + e.getMessage());
        }
        return response;
    }

    // 管理员端 护工管理 服务关注 已购护理项目表格 弹窗管理护理项目 右侧续费按钮
    @PostMapping("/updateClientNursingProject")
    public ResponseBean<Integer> updateClientNursingProject(ClientNursingProject clientNursingProject) {
        Integer result;
        ResponseBean<Integer> rb = null;
        UpdateWrapper<ClientNursingProject> uw = new UpdateWrapper<>();
        uw.eq("c_l_id", clientNursingProject.getCLId());
        try{
            result = clientNursingProjectDAO.update(clientNursingProject, uw);
            rb = new ResponseBean<>(result);
        }catch (Exception e){
            e.printStackTrace();
            rb =new ResponseBean<>(500,"修改客户及其护理等级及其护理项目错误");
        }
        return rb;
    }

    //管理员端 健康管家 服务关注 护理项目管理 判断是否到期
    @PostMapping("/judgeNursingProjectExpiration")
    public ResponseBean<Integer> judgeNursingProjectExpiration(Long clientId, Long levelId, Long projectId) {
        return clientNursingProjectService.judgeNursingProjectExpiration(clientId, levelId, projectId);
    }

    //管理员端 健康管家 服务关注 护理项目管理 判断是否欠费
    @PostMapping("/judgeNursingProjectCost")
    public ResponseBean<Integer> judgeNursingProjectCost(Long clientId, Long levelId, Long projectId) {
        return clientNursingProjectService.judgeNursingProjectCost(clientId,levelId,projectId);
    }

    //管理员端 健康管家 服务关注 护理项目管理 判断是否快要到期且数量正常
    @GetMapping("/judgeNursingProject")
    public ResponseBean<List<ClientAndProject>> judgeNursingProject() {
        return clientNursingProjectService.judgeNursingProject();
    }
}
